<?php

/**
 * This is the model class for table "families".
 *
 * The followings are the available columns in table 'families':
 * @property integer $id
 * @property integer $employeeId
 * @property string $name
 * @property string $gender
 * @property string $relation
 */
class Family extends ActiveRecordUser {

	const RELATION_SPOUSE = 'Spouse';
	const RELATION_CHILD = 'Child';

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Family the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'families';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('employeeId, name', 'required'),
			array('employeeId', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>128),
			array('gender, relation', 'length', 'max'=>8),
			array('employeeId, name, gender, relation', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'employee'=>array(self::BELONGS_TO, 'Employee', 'employeeId'),
			'insurances'=>array(
				self::HAS_MANY,
				'Insurance',
				'ownerId',
				'condition'=>'ownerClass = :ownerClass',
				'params'=>array(':ownerClass'=>__CLASS__),
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id'=>'ID',
			'employeeId'=>'Employee',
			'name'=>'Name',
			'gender'=>'Gender',
			'relation'=>'Relation',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('employeeId', $this->employeeId);
		$criteria->compare('name', $this->name, true);
		$criteria->compare('gender', $this->gender, true);
		$criteria->compare('relation', $this->relation, true);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
			));
	}

	public function behaviors() {
		return array(
			'dataProvider'=>array(
				'class'=>'application.behaviors.DynamicDataProviderBehavior',
			),
			'genderBehavior'=>array(
				'class'=>'application.behaviors.GenderListBehavior',
				'attribute'=>'gender',
			),
		);
	}
	
	public function getMenu($action, $toolbar = false) {
		$name = 'Family';
		$user = Yii::app()->user;
		$tool = Yii::app()->request->baseUrl . '/images/toolbars';
		$menu = array();

		$label = "View $name";
		if ($toolbar) $label = "<img src=\"$tool/magnifier-left.png\" />";
		$menu[] = array(
			'label'=>$label,
			'url'=>array('view', 'id'=>$this->id),
			'visible'=>$action != 'view' && $user->checkAccess("$name.View"),
			'linkOptions'=>array('title'=>'View'),
		);

		$label = "Update $name";
		if ($toolbar) $label = "<img src=\"$tool/pencil.png\" />";
		$menu[] = array(
			'label'=>$label,
			'url'=>array('update', 'id'=>$this->id),
			'visible'=>$action != 'update' &&
			$user->checkAccess("$name.Update"),
			'linkOptions'=>array('title'=>'Update'),
		);

		$insurance = Insurance::model();
		$label = "Add Insurance";
		if ($toolbar) $label = "<img src=\"$tool/wall--plus.png\" />";
		$menu[] = array(
			'label'=>$label,
			'url'=>array(
				'insurance/create',
				'ownerClass'=>__CLASS__,
				'ownerId'=>$this->id,
			),
			$user->checkAccess('Insurance.Create'),
			'linkOptions'=>array('title'=>'Add Insurance'),
		);

		$label = "List Insurance";
		if ($toolbar) $label = "<img src=\"$tool/wall.png\" />";
		$menu[] = array(
			'label'=>$label,
			'url'=>array(
				'insurance/index',
				CHtml::activeName($insurance, 'ownerClass')=>__CLASS__,
				CHtml::activeName($insurance, 'ownerId')=>$this->id,
			),
			$user->checkAccess('Insurance.Index'),
			'linkOptions'=>array('title'=>'List Insurance'),
		);

		$label = "Delete $name";
		if ($toolbar) $label = "<img src=\"$tool/cross-script.png\" />";
		$menu[] = array(
			'label'=>$label,
			'url'=>'#',
			'visible'=>$user->checkAccess("$name.Delete"),
			'linkOptions'=>array(
				'submit'=>array('delete', 'id'=>$this->id),
				'confirm'=>'Are you sure you want to delete this item?',
				'title'=>'Delete',
			)
		);
		return $menu;
	}

}